package com.software.task.controller;

import com.software.task.anno.Debounce;
import com.software.task.mapper.UserMapper;
import com.software.task.pojo.Result;
import com.software.task.pojo.Task;
import com.software.task.pojo.User;
import com.software.task.service.CategoryService;
import com.software.task.service.TaskService;
import jakarta.annotation.Resource;



import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author 李嘉
 * @since 2024/12/15 11.34
 * @version 1.0
 */
@RestController
@RequestMapping("/tasks")
public class TaskController {

    @Resource
    private TaskService taskService;

    @Resource
    private UserMapper userMapper;

    @Resource
    private CategoryService categoryService;

    /**
     * 根据任务id获取任务
     *
     * @param taskid 任务id
     * @return 任务信息
     */
    @Debounce()
    @GetMapping("/{taskid}")
    public Result<?> getTaskById(@PathVariable Integer taskid) {
        Optional<Task> optionalTask = Optional.ofNullable(taskService.getTaskById(taskid));
        return optionalTask.map(Result::success).orElse(Result.error("没有相关课程"));
    }

    /**
     * 根据任务名称获取任务
     * @param name 任务名称
     * @return 任务信息
     */
    @Debounce()
    @GetMapping("/name")
    public Result<List<Task>> getTasksByName(@RequestParam String name) {
        List<Task> tasks = taskService.getTasksByName(name);
        return Result.success(tasks);
    }

    /**
     * 获取教师发布的任务
     * @param teacherId 教师id
     * @return 任务信息
     */
    @Debounce()
    @GetMapping("/teacher/{teacherId}")
    public Result<List<Task>> getTasksByTeacherId(@PathVariable Integer teacherId) {
        List<Task> tasks = taskService.getTasksByTeacherId(teacherId);
        return Result.success(tasks);
    }

    /**
     * 获取学生参与的任务
     * @param studentId      学生id
     * @return  任务信息
     */
    @Debounce()
    @GetMapping("/student/{studentId}")
    public Result<List<Task>> getTasksByStudentId(@PathVariable Integer studentId) {
        List<Task> tasks = taskService.getTasksByStudentId(studentId);
        return Result.success(tasks);
    }

    /**
     * 创建任务
     * @param task 任务信息
     * @return 任务信息
     */
    @Debounce()
    @PostMapping
    public Result<?> createTask(@RequestBody Task task) {
        taskService.createTask(task);
        // 如果需要，可以返回新创建的任务信息，包括自动生成的 taskid
        return Result.success(task);
    }

    /**
     * 删除任务
     * @param id 任务id
     * @return 成功或失败
     */
    @Debounce()
    @DeleteMapping("/{id}")
    public Result<?> deleteTaskById(@PathVariable Integer id) {
        taskService.deleteTaskById(id);
        categoryService.deleteById(id);
        return Result.success();
    }

    /**
     * 更新任务信息
     * @param id 任务id
     * @param taskDetails 任务信息
     * @return 成功或失败
     */
    @Debounce()
    @PutMapping("/{id}")
    public Result<?> updateTaskById(@PathVariable Integer id, @RequestBody Task taskDetails) {
        taskService.updateTaskById(id, taskDetails);
        return Result.success();
    }


    /**
     * 获取所有任务
     * @return 任务信息
     */
    @Debounce()
    @GetMapping
    public Result<List<Task>> getAllTasks() {
        List<Task> tasks = taskService.findAllTasks();
        tasks.forEach(task -> {
            User user = userMapper.selectById(task.getTeacherId());
            if (user != null) {
                task.setTeacherName(user.getUsername());
            }
        });
        return Result.success(tasks);
    }

}